package com.woniu113.junk.site.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu113.junk.commons.model.Result;
import com.woniu113.junk.commons.util.RedisUtil;
import com.woniu113.junk.commons.util.TokenUtil;
import com.woniu113.junk.site.dto.AllWasteTransferStatisticsDTO;
import com.woniu113.junk.site.dto.WasteTransferStatisticsDTO;
import com.woniu113.junk.site.form.wastetransfer.AddWasteTransferForm;
import com.woniu113.junk.site.form.wastetransfer.FindWasteTransferForm;
import com.woniu113.junk.site.model.WasteTransferStatistics;
import com.woniu113.junk.site.param.wastetransfer.AddWasteTransferParam;
import com.woniu113.junk.site.param.wastetransfer.FindWasteTransferParam;
import com.woniu113.junk.site.service.WasteTransferStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  废品转移控制器
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@RestController
@RequestMapping("/wasteTransferStatistics")
@Api(value = "WasteTransferStatisticsController", tags = "废品转移信息接口")
public class WasteTransferStatisticsController {
    @Resource
    private WasteTransferStatisticsService wasteTransferStatisticsService;
    @Resource
    private RedisUtil redisUtil;

    @PostMapping("/addWasteTransfer")
    @ApiOperation(value = "新增废品转移信息")
    public Result<WasteTransferStatistics> addWasteTransfer(@RequestBody AddWasteTransferForm form, @RequestHeader String token) throws Exception{
        System.out.println("骑手id"+form.getDoId());
        System.out.println("站点id"+form.getStationId());
        System.out.println("区站点"+form.getCountyStationId());
        System.out.println("废品"+form.getWasteList());
        //获取当前登录管理员的id
        int adminId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        System.out.println(adminId);
        //调用方法
        WasteTransferStatistics newWasteTransferStatistics = wasteTransferStatisticsService.addWasteTransfer(BeanUtil.toBean(form, AddWasteTransferParam.class),adminId);
        return Result.getResult(WasteTransferStatistics.class).setData(newWasteTransferStatistics).setMsg("新增转移信息成功").setCode(200);
    }

    @GetMapping("/transferStart")
    @ApiOperation(value = "骑手开始转移")
    public Result transferStart(@RequestParam("transferId") Integer transferId,@RequestHeader String token) throws Exception{
        //获取当前登录管理员的id
        int adminId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        System.out.println(adminId);
        //调用方法
        wasteTransferStatisticsService.transferStart(transferId,adminId);
        return Result.getResult().setMsg("骑手开始转移成功").setCode(200);
    }

    @PostMapping("/findWasteTransfer")
    @ApiOperation(value = "按条件查询废品转移信息")
    public Result<List<WasteTransferStatisticsDTO>> findWasteTransfer(@RequestBody FindWasteTransferForm form){
        int offset = (form.getPageNum() - 1) * form.getPageSize();
        AllWasteTransferStatisticsDTO dto = wasteTransferStatisticsService.findWasteTransfer(BeanUtil.toBean(form, FindWasteTransferParam.class),offset);
        Integer total = dto.getTotal();
        return Result.getListResult(WasteTransferStatisticsDTO.class).setData(dto.getList()).setMsg("按条件查询废品转移信息成功").setCode(200).setTotal(dto.getTotal());
    }
}

